import unittest
import engine
import test_util

class EngineTest(unittest.TestCase):
    """Test case for the 'engine' module"""
    def setUp(self):
        "setup goes here"
        self.l1 = DummyListener()
        self.l2 = DummyListener()
    
    def test_listeners(self):
        e = engine.get_simulation()
        
        e.add_turn_listener(self.l1, engine.EVENTS)
        
        e.do_turn()
        
        self.assertTrue(self.l1.acknowledge())
        self.assertEquals(self.l1.last_rank, engine.EVENTS)
        
        e.add_turn_listener(self.l2, engine.UI)
        
        e.do_turn()
        
        self.assertTrue(self.l1.acknowledge())
        self.assertEquals(self.l1.last_rank, engine.EVENTS)
        
        self.assertTrue(self.l2.acknowledge())
        self.assertEquals(self.l2.last_rank, engine.UI)
        
        
        e.add_turn_listener(self.l2, engine.UPDATES)
        
        e.do_turn()
        
        self.assertTrue(self.l1.acknowledge())
        self.assertEquals(self.l1.last_rank, engine.EVENTS)
        
        self.assertTrue(self.l2.acknowledge(2))
        self.assertEquals(self.l2.last_rank, engine.UI)
        
       
        
    def tearDown(self):
        engine.get_simulation().clear_listeners()
        
    


class DummyListener(test_util.Listener):
    
    def __init__(self):
        test_util.Listener.__init__(self)
        
    
    def do_turn(self, rank): 
        self.receive()
        self.last_rank = rank

if __name__ == '__main__':
    unittest.main()

def get_suite():
    return unittest.makeSuite(EngineTest)
